package com.ccjq.shosestore.core.dbmanager;

import java.sql.*;

public class DbConnection {
    private static final String DRIVER;
    private static final String URL;
    private static final String USERNAME;
    private static final String PASSWORD;

    /*静态代码块*/
    static {
        DbConfigReader dbConfigReader = new DbConfigReader();
        DRIVER = dbConfigReader.getValue("driver");
        URL = dbConfigReader.getValue("url");
        USERNAME = dbConfigReader.getValue("username");
        PASSWORD = dbConfigReader.getValue("password");
    }

    /*建立数据库连接*/
    public static Connection getConnection() {
        Connection con = null;
        PreparedStatement pre = null;
        ResultSet res = null;
        try {
            Class.forName(DRIVER);
            con = DriverManager.getConnection(URL, USERNAME, PASSWORD);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return con;
    }

    public static void close(ResultSet res, PreparedStatement sta, Connection con) {

        try {
            if (res != null) {

                res.close();
            }
            if (sta != null) {
                sta.close();
            }
            if (con != null) {
                con.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
